<?php
class Admin extends Controller{
    public $data;
    function __construct(){
        parent::__construct();
        Session::init();
        $this->model("model_admin");
    }

    public function index(){
        if (Session::get("uid")){


            $this->data['view'] = "index";
        }else{
            header('location:' . ROOT_URL."admin/login");
        }
        $this->data['menu'] = "index";
        $this->data['category'] = "Home";
        $this->data['description'] = "Home page management";
        $this->data['data'] = "";
        $this->view->render("admin/layout/body", $this->data);
    }

    public function login(){
        if (Session::get("uid")){
            header('location:' . ROOT_URL."admin");
        }else{
            if(!empty($_POST)){
                $username = $_POST['username'];
                $password = md5($_POST['password']);
                $user = $this->model_admin->getUserByUsernamePassword($username, $password);
                if($user){
                    Session::set("uid", $user[0]['uid']);
                    Session::commit();
                }else{
                    $this->data['error'] = "Login error, Please try again!";
                }
            }
            $this->data['view'] = "login";
        }
        $this->data['data'] = "";
        $this->view->render("admin/login", $this->data);
    }

    public function logout(){

    }

    //Users
    public function users(){
        if (Session::get("uid")){
            $this->data['view'] = "users";
        }else{
            header('location:' . ROOT_URL."admin/login");
        }
        $this->data['menu'] = "users";
        $this->data['category'] = "Users";
        $this->data['description'] = "Management users";
        $this->data['data'] = "";
        $this->view->render("admin/layout/body", $this->data);
    }

    public function sign_up(){
        if (Session::get("uid")){


            $this->data['view'] = "sign_up";
        }else{
            header('location:' . ROOT_URL."admin/login");
        }
        $this->data['menu'] = "users";
        $this->data['category'] = "Users";
        $this->data['description'] = "Sign up new user";
        $this->data['data'] = "";
        $this->view->render("admin/layout/body", $this->data);
    }

    public function profile(){
        if (Session::get("uid")){
            $this->data['view'] = "profile";
        }else{
            header('location:' . ROOT_URL."admin/login");
        }
        $this->data['menu'] = "users";
        $this->data['category'] = "Users";
        $this->data['description'] = "Profile user";
        $this->data['data'] = "";
        $this->view->render("admin/layout/body", $this->data);
    }

    public function password(){
        if (Session::get("uid")){
            $this->data['view'] = "password";
        }else{
            header('location:' . ROOT_URL."admin/login");
        }
        $this->data['menu'] = "users";
        $this->data['category'] = "Users";
        $this->data['description'] = "Update user password";
        $this->data['data'] = "";
        $this->view->render("admin/layout/body", $this->data);
    }

    //Articles
    public function articles(){
        if (Session::get("uid")){
            $this->data['view'] = "articles";
        }else{
            header('location:' . ROOT_URL."admin/login");
        }
        $this->data['menu'] = "articles";
        $this->data['category'] = "Articles";
        $this->data['description'] = "Management articles";
        $this->data['data'] = "";
        $this->view->render("admin/layout/body", $this->data);
    }

    public function create_new_article(){
        if (Session::get("uid")){

            $this->data['view'] = "create_new_article";
        }else{
            header('location:' . ROOT_URL."admin/login");
        }
        $this->data['view'] = "create_new_article";
        $this->data['menu'] = "articles";
        $this->data['category'] = "Articles";
        $this->data['description'] = "Create new article";
        $this->data['data'] = "";
        $this->view->render("admin/layout/body", $this->data);
    }

    public function article_adjust(){
        if (Session::get("uid")){

            $this->data['view'] = "article_adjust";
        }else{
            header('location:' . ROOT_URL."admin/login");
        }
        $this->data['menu'] = "articles";
        $this->data['category'] = "Articles";
        $this->data['description'] = "Article adjust";
        $this->data['data'] = "";
        $this->view->render("admin/layout/body", $this->data);
    }

    //Themes
    public function themes(){
        if (Session::get("uid")){


            $this->data['view'] = "themes";
        }else{
            header('location:' . ROOT_URL."admin/login");
        }
        $this->data['menu'] = "themes";
        $this->data['category'] = "Themes";
        $this->data['description'] = "Management themes";
        $this->data['data'] = "";
        $this->view->render("admin/layout/body", $this->data);
    }

    public function create_new_theme(){
        if (Session::get("uid")){

            $this->data['view'] = "create_new_theme";
        }else{
            header('location:' . ROOT_URL."admin/login");
        }
        $this->data['menu'] = "themes";
        $this->data['category'] = "Themes";
        $this->data['description'] = "Create new themes";
        $this->data['data'] = "";
        $this->view->render("admin/layout/body", $this->data);
    }

    public function theme_adjust(){
        if (Session::get("uid")){


            $this->data['view'] = "themes_adjust";
        }else{
            header('location:' . ROOT_URL."admin/login");
        }
        $this->data['menu'] = "themes";
        $this->data['category'] = "Themes";
        $this->data['description'] = "Themes adjust";
        $this->data['data'] = "";
        $this->view->render("admin/layout/body", $this->data);
    }

    //Categories
    public function categories(){
        if (Session::get("uid")){
            //add category
            if(!empty($_POST)){
                $title = $_POST['title'];
                $url = mb_strtolower($this->libs->url_title($this->libs->removeSign($title)));
                $position = $_POST['position'];
                $parent = $_POST['parent'];
                $description = $_POST['description'];
                if($title != ""){
                    $this->model_admin->addCategory($title, $url, $parent, $position, $description);
                }
            }

            $data = $category = $this->model_admin->getCategoryByParentId(0);
            for($i = 0; $i < sizeof($data); $i++){
                $categoryChild = $this->model_admin->getCategoryByParentId($data[$i]['cid']);
                if($categoryChild){
                    $data[$i]['children'] = $categoryChild;
                }
            }
            $this->data['parent_category'] = $category;

            $this->data['view'] = "categories";
            $this->data['menu'] = "categories";
            $this->data['category'] = "Categories";
            $this->data['description'] = "Management categories";
            $this->data['data'] = $data;
            $this->view->render("admin/layout/body", $this->data);
        }else{
            header('location:' . ROOT_URL."admin/login");
        }
    }

    /*
     * Type: Uploader function
     * Coder: Tuan Do
     * Date: 12-10-2014
     */



}
?>